package com.lanhai.javaBean;

import java.awt.geom.GeneralPath;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.lanhai.model.ControlArea;
import com.lanhai.model.ControlRunAgv;
import com.lanhai.utils.RegionUtil;
import com.lanhai.utils.StringUtil;
import com.mybatis.dao.ControlAreaDao;

public class ControlAreaBean {

	/**
	 * 加载交管区列表
	 */
	public static List<ControlArea> loadData(SqlSessionFactory sqlSessionFactory){
		List<ControlArea> controlAreaList = new ArrayList<ControlArea>();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try{
			ControlAreaDao caDao = sqlSession.getMapper(ControlAreaDao.class);
			List<Map<String, Object>> list = caDao.selectAll();
			if(list.size() > 0){
				for(Map<String, Object> m:list){
					ControlArea ca = new ControlArea();
					ca.setId((Integer)m.get("id"));
//					ca.setOutPark1((Integer)m.get("outPark1"));
//					ca.setOutPark2((Integer)m.get("outPark2"));
					ca.setName((String)m.get("name"));
					ca.setFlag(1);//可通行
					ca.setBaseX((Double)m.get("baseX"));
					ca.setBaseY((Double)m.get("baseY"));
					ca.setIpAddress((String)m.get("ipAddress"));//IP地址
					ca.setType((Integer)m.get("type"));//交管区类型：0，独占模式  1：分线路锁管理模式
//					ca.setJoinCaId((Integer)m.get("joinCaId"));//关联交管区ID
//					ca.setStopFlag((Integer)m.get("stopFlag"));
					String xStr = (String)m.get("mapXArr");
					boolean flag = false;
					if(xStr != null){
						Double[] mapXArr = StringUtil.convertDoubleArray(xStr);
						ca.setMapXArr(mapXArr);
						flag = true;
					}
					String yStr = (String)m.get("mapYArr");
					if(yStr != null){
						Double[] mapYArr = StringUtil.convertDoubleArray(yStr);
						ca.setMapYArr(mapYArr);
					}
					if(flag){
						GeneralPath shape = RegionUtil.getCtrlShape(ca.getMapXArr(), ca.getMapYArr());
						ca.setShape(shape);
					}
					ca.setRunAgvList(new ArrayList<ControlRunAgv>());
					controlAreaList.add(ca);					
				}
			}
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	sqlSession.close();
	    }
        return controlAreaList;
	}
	
}
